package com.coder.oj.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.oj.admin.mapstruct.UserMapstruct;
import com.coder.oj.admin.query.UserQuery;
import com.coder.oj.admin.vo.UserVo;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.core.R;
import com.coder.oj.common.entity.User;
import com.coder.oj.common.enums.CommonStatusEnum;
import com.coder.oj.common.exception.DataException;
import com.coder.oj.common.mapper.UserMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserMapstruct userMapstruct;

    /**
     * 分页查询
     * @author Bosen 2022/12/16 10:06
     **/
    public JsonPage<UserVo> getUserPage(UserQuery params) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(params.getNickname()), User::getNickname, params.getNickname());
        queryWrapper.eq(ObjectUtils.isNotEmpty(params.getStatus()), User::getStatus, params.getStatus());
        IPage<User> page = userMapper.selectPage(new Page<>(params.getPage(), params.getPageSize()), queryWrapper);
        List<UserVo> collect = page.getRecords().stream().map(userMapstruct::toVO).collect(Collectors.toList());
        return new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
    }

}
